package service.impl;


import dao.mapper.UserMapper;
import models.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import service.UserService;

import java.io.IOException;
import java.io.InputStream;

public class UserServiceImpl implements UserService {


    private UserMapper userMapper;

    @Override
    public User register(String userName, String password, String email) {



        InputStream is= null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user=new User();
        user.setUid(600);
        user.setUname(userName);
        user.setPwd(password);
        user.setEmail(email);
        user.setPhone("11");

        user.setSex("0");
        user.setAge(1);
        int result=userMapper.InsertUser(user);
        sqlSession.commit();
        System.out.println("result"+result);
        sqlSession.close();
       /* // 创建一个新的 User 对象
        User newUser = new User();
        newUser.setUname(userName);
        newUser.setPwd(password);
        newUser.setEmail(email);
        newUser.setAge(0);
        newUser.setPhone("123");
        newUser.setSex("1");
        newUser.setUid(0);
        newUser.setStatus(0);
        // 在数据库中插入新用户
        int rowsAffected = userMapper.InsertUser(newUser);

        // 根据插入结果判断注册是否成功
        if (rowsAffected > 0) {
            // 注册成功，返回新用户对象
            return newUser;
        } else {
            // 注册失败，可以根据需要抛出异常或者返回 null
            return null;
        }

*/
        return user;

    }
    @Override
    public User getUserByUsername(String username) {
        return userMapper.GetUserByUsername(username);
    }


    @Override
    public boolean login(String userName, String password) {

            boolean isAuthenticated= false;

            // 判断是否有匹配的用户
      //      isAuthenticated = resultSet.next();
            return isAuthenticated;


    }
    @Override
    public String getUserPassword(String name) {
        // 调用 userMapper 的方法，根据用户名获取用户密码
        String password = userMapper.GetUserPassword(name);

        // 判断密码是否为 null，如果为 null 则返回 null，否则返回实际密码
        return password != null ? password : null;
    }

    @Override
    public boolean retrievePassword(String userName, String email) {
//            //验证码
//            boolean verification=false;
//            boolean isAuthenticated= false;
//            if(isAuthenticated && verification){
//                //写入密码到数据库
//                return true;
//            }else {
//                return false;
//            }
        return true;
    }
    @Override
    public void banUser(Integer userId) {
            userMapper.ChangeStatus(userId,false);
    }

    @Override
    public void unbanUser(Integer userId) {
            userMapper.ChangeStatus(userId,true);
    }
    @Override
    public void deleteUser(Integer userId) {
            //数据库中删除

    }
    @Override
    public void viewPhoto(String photoID) {

    }
    @Override
    public void likePhoto(String photoID) {
//        Photo photo=null;
//        //数据库查找
//        photo.getLike()
//        photo.setLike(1);
    }
    @Override
    public void commentPhoto(String photoID, String comment) {
//        Photo photo=null;
//        //
//        Comment[] comment1=photo.getComment();
//        CommentService commentService;
//        commentService.add
//        photo.setComment(comment);
    }
    @Override
    public void appealBan(String userId, String appeal) {
        //数据库
    }
    @Override
    public void reportPhoto(String photoID, String userId) {

    }
    @Override
    public void reportComment(String commentID, String userId) {


    }

    @Override
    public int getUserNum(){
        return userMapper.GetUserNum();

    }

    @Override
    public User getUserByUid(Integer uid) {
        return userMapper.GetUserByUid(uid);
    }

    @Override
    public String getUserData(String name){
        String password = userMapper.GetUserPassword(name);
        String email = userMapper.GetUserEmail(name);
        boolean status = userMapper.GetUserStatus(name);
        // 构建字符串格式
        String formattedString = String.format("{\n" +
                "  \"username\": \"%s\",\n" +
                "  \"password\": \"%s\",\n" +
                "  \"email\": \"%s\",\n" +
                "  \"banStatus\": \"%s\"\n" +
                "}", name, password, email, status);
        return formattedString;
    }
    @Override
    public int getUseridByname(String name)
    {
        int uid=userMapper.GetUserIdByName(name);
        return uid;
    }
    @Override
    public void upDataUser(User user)
    {
        userMapper.UpdataUser(user);
    }
}
